package com.ruku.mapper;

import com.bean.*;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

public interface RuKuMapper {
    //入库办理
    @Select("select * from wms_t_customer where custName like ('${value}%')")
    public List<WmsTCustomer> getCusMess(String shuru);


    //办理入库插入wms_t_bill表
    @Insert("insert into wms_t_bill (customerID,billID,billTransactor,billDate,billEditTime,billFeeStore,billFeeTruckage,billFeeOther,billPayable,billPaid,billIsDel,billState,billType,billNote) " +
            "values(#{customerID},#{billID},#{billTransactor},#{billDate},#{billEditTime},#{billFeeStore},#{billFeeTruckage},#{billFeeOther},#{billPayable},#{billPayable},'0','0','0','0')")
    public void insertRuku(WmsTBill wmsTBill);

    //根据输入货主姓名获取customerID
    @Select("Select custID from wms_t_customer where custName=#{value}")
    public WmsTCustomer getCustID(String custName);

    @Select("select * from wms_t_store where wms_t_store.storeID in(\n" +
            "select storeID from wms_t_site WHERE wms_t_site.siteID in(\n" +
            "select siteID from wms_t_tray where wms_t_tray.trayState = '0'));")
    public List<WmsTStore> selectStore();

    @Select("select * from wms_t_site WHERE storeID = #{value} and wms_t_site.siteID in(\n" +
            "select siteID from wms_t_tray where wms_t_tray.trayState = '0');")
    public List<WmsTSite> selectSite(String storeID);


    //显示所有信息
    @Select("select * from  wms_t_bill  left join wms_t_customer on  wms_t_bill.customerID =wms_t_customer.custID where billType=0 and billState=0")
    @Results({@Result(column = "customerID", property = "wmsTCustomer", one = @One(select = ("com.ruku.mapper.RuKuMapper.selectCus")))})
    public List<WmsTBill> selectAll();

    @Select("select * from wms_t_customer where custID= #{custID}")
    public WmsTCustomer selectCus(String custID);

    //装盘明细
    @Select("select * from wms_t_bill_details where billId=#{billID}")
    public List<WmsTBillDetails> selectBillDetail(String billId);

    //根据billId更新billState为0
    @Update("update wms_t_bill set billState=1 where billID=#{billID}")
    public void UpdateBill(String billID);

    //批量删除装盘明细
    @Delete("delete from wms_t_bill_details where detailsID=#{value}")
    public void deleteMany(String detailsID);
//    @Delete(" delete from wms_t_bill_details where detailsID in\n" +
//            "        <foreach collection=\"list\" item=\"item\" open=\"(\" separator=\",\" close=\")\" >\n" +
//            "            #{item}\n" +
//            "        </foreach>")


    //费用结算更新到数据库
    @Update("update wms_t_bill set billFeeTruckage=#{billFeeTruckage},billFeeOther=#{billFeeOther},billPayable=#{billPayable},billPaid=#{billPaid},billState=2 where billID=#{billID}")
    public void feiyong(WmsTBill wmsTBill);

    //点击入库确认的时候更新wms_t_bill 的billState = 3
    @Update("update wms_t_bill set billState=3 where billID=#{value}")
    public void rukuqueren(String billID);


    //入库单查询
    @Select("    <script>\n" +
            "        select wms_t_bill.*,wms_t_customer.custName from wms_t_bill LEFT JOIN wms_t_customer on wms_t_bill.customerID =\n" +
            "        wms_t_customer.custID WHERE billType=0 and billState=3 and billIsDel=0\n" +
            "        <if test=\"custName != null and custName != ''\">\n" +
            "            and wms_t_bill.customerID in(SELECT custID FROM wms_t_customer WHERE wms_t_customer.custName = #{custName})\n" +
            "        </if>\n" +
            "        <if test=\"start != null and start != ''\">\n" +
            "            <if test=\"end != null and end != ''\">\n" +
            "                and wms_t_bill.billDate BETWEEN #{start} AND #{end}\n" +
            "            </if>\n" +
            "        </if>\n" +
            "        <if test=\"billID != null and billID != ''\">\n" +
            "            and wms_t_bill.billID = #{billID}\n" +
            "        </if>\n" +
            "    </script>\n")
    public List<Map<String, Object>> bill(String start, String end, String custName, String billID);


    //入库单信息
    @Select("select * from wms_t_bill where billID = #{value}")
    @Results(@Result(column = "customerID", property = "wmsTCustomer", one = @One(select = "com.ruku.mapper.RuKuMapper.selectcust")))
    public WmsTBill selectbills(String billID);

    @Select("select * from wms_t_customer where custID = #{value}")
    public WmsTCustomer selectcust(String custID);


    //删除入库单信息
    @Update("update wms_t_bill set billIsDel=1 where billID=#{value}")
    public void deleteMess(String billID);

    //根据custName获取custID
    @Select("select custID from wms_t_customer where custName=#{value}")
    public WmsTCustomer selectCustID(String custName);

    //根据billID更新入库单到数据库
    @Update("update wms_t_bill set customerID=#{customerID},billFeeTruckage=#{billFeeTruckage},billDate=#{billDate},billEditTime=#{billEditTime} where billID=#{billID}")
    public void updateMess(String billDate,String billEditTime,double billFeeTruckage,String billID,String customerID);

    //计算总费用
    @Update("update wms_t_bill set billPayable=(billFeeStore*30 + billFeeTruckage + billFeeOther),billPaid=(billFeeStore*30 + billFeeTruckage + billFeeOther) where billID=#{value}")
    public void jisuan(String billID);
}